﻿using NHibernateDemo.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using NHibernate;
using NHibernateDemo.DAL.Entity;
using log4net;
using System.IO;
using System.Collections.Generic;
using Arich.Data.GenericNHDal;
namespace NHibernateDemo.DAL.Test
{
    
    
    /// <summary>
    ///This is a test class for CategoryDALTest and is intended
    ///to contain all CategoryDALTest Unit Tests
    ///</summary>
    [TestClass( )]
    public class CategoryDALTest {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
            }
            set {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestInitialize]
        public void Setup( ) {
            log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize( ); 
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod( )]
        public void DeleteTest( ) {
            
            ISession session = null; // TODO: Initialize to an appropriate value
            CategoryDAL target = new CategoryDAL(); // TODO: Initialize to an appropriate value
            Category category = new Category { CategoryId = "$$xxxx12$$", Name = "Category2" };
            target.Save(category);
            //Category category = target.GetById("$$xxxx12$$"); // TODO: Initialize to an appropriate value
            target.Delete(category);
            Assert.IsNull(target.GetById(category.CategoryId));
            //Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for SafeAdd
        ///</summary>
        [TestMethod( )]
        public void Create_DeleteTest( ) {
            ISession session = null; // TODO: Initialize to an appropriate value
            CategoryDAL target = new CategoryDAL(); // TODO: Initialize to an appropriate value
            Category category = new Category { CategoryId = "$$xxxx12$$", Name = "Category2" }; // TODO: Initialize to an appropriate value
           
            category.Funds.Add(new FundBasic { FundId = "OF123123xx", FundName = "Fund2",Category = category });
            category.Funds.Add(new FundBasic { FundId = "OF888888xx", FundName = "Fund3", Category = category });
            target.Delete(category);
            target.Save(category);
            var newC = target.GetById(category.CategoryId);
            Assert.AreEqual(category.CategoryId, newC.CategoryId);
            //Assert.IsTrue(newC.Funds.Count > 0);
            target.Delete(newC);
            Assert.IsNull(target.GetById(category.CategoryId));
            //Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }



        /// <summary>
        ///A test for Update
        ///</summary>
        [TestMethod( )]
        public void UpdateTest( ) {
            ISession session = null; // TODO: Initialize to an appropriate value
            CategoryDAL target = new CategoryDAL(); // TODO: Initialize to an appropriate value
            Category category = new Category { CategoryId = "$$xxxx12$$", Name = "Category2" }; // TODO: Initialize to an appropriate value
            category.Funds.Add(new FundBasic { FundId = "OF123123xx", FundName = "Fund2",Category = category });
            target.Delete(category);
            target.Save(category);
            category.Name = "newCategory";
            category.Funds.Add(new FundBasic { FundId = "OE123123xx", FundName = "Fund3", Category = category });
            target.SaveOrUpdate(category);
            var newC = target.GetById("$$xxxx12$$");
            Assert.AreEqual(category.CategoryId, newC.CategoryId);
            Assert.AreEqual("newCategory", newC.Name);
            Assert.AreEqual(2, newC.Funds.Count);
            target.Delete(newC);
        }

        /// <summary>
        ///A test for SearchByName
        ///</summary>
        [TestMethod( )]
        public void SearchByNameTest( ) {
            ISession session = null; // TODO: Initialize to an appropriate value
            CategoryDAL target = new CategoryDAL(); // TODO: Initialize to an appropriate value
            Category category = new Category { CategoryId = "$$xxxx12$$", Name = "Category2" }; // TODO: Initialize to an appropriate value
            category.Funds.Add(new FundBasic { FundId = "OF123123xx", FundName = "Fund2", Category = category });
            target.Delete(category);
            target.Save(category);

            string name = "Category2"; // TODO: Initialize to an appropriate value
            IList<Category> expected = null; // TODO: Initialize to an appropriate value
            IList<Category> actual;
            actual = target.SearchByName(name);
            Assert.AreEqual(1, actual.Count);
            Assert.AreEqual(1, actual[0].Funds.Count);
            target.Delete(category);
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for SearchByLinkName
        ///</summary>
        [TestMethod( )]
        public void SearchByLinkNameTest( ) {
            ISession session = null; // TODO: Initialize to an appropriate value
            CategoryDAL target = new CategoryDAL(); // TODO: Initialize to an appropriate value
            string name = "Cate"; // TODO: Initialize to an appropriate value
            Category category = new Category { CategoryId = "$$xxxx12$$", Name = "Category2" }; // TODO: Initialize to an appropriate value
            category.Funds.Add(new FundBasic { FundId = "OF123123xx", FundName = "Fund2", Category = category });
            target.Delete(category);
            target.Save(category);
            IList<Category> expected = null; // TODO: Initialize to an appropriate value
            IList<Category> actual;
            actual = target.SearchByLikeName(name);
            Assert.AreEqual(1, actual.Count);
            target.Delete(category);
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }


        [TestMethod]
        public void TestSessionManageEntity( ) {
            ISession session = NHibernateSessionManager.Instance.GetSession( );
            Category category = new Category { CategoryId = "$$xxxx12$$", Name = "Category2" };
            CategoryDAL target = new CategoryDAL();
            target.Save(category);
            PersonBasic person = new PersonBasic { Age = 10, Name = new Name { LastName = "fund", FirstName = "123" } };
            session.Save(person);
            session.Flush();
            target.Delete(category);
            session.Evict(person);
            PersonBasicDAL personDAL = new PersonBasicDAL( );
            personDAL.Delete(person);
        }
    }
}
